﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace aPCSuite
{
    public partial class frmFake : Form
    {
        private SMSFramework _SMSFramework = new SMSFramework();
        public event RefereshSMS _refresh_sms = null;
        public frmFake()
        {
            InitializeComponent();
            _SMSFramework.parent = this;
            this.Move += new EventHandler(MoveSubForm);

            txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.Refresh();

            ContextMenu cm = new ContextMenu();
            MenuItem mi = new MenuItem("Paste");
            mi.Click += new EventHandler(PasteAction);
            cm.MenuItems.Add(mi);
            richTextBox1.ContextMenu = cm;
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                richTextBox1.Text
                    += Clipboard.GetText(TextDataFormat.Text).ToString();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string snumber = txtTo.Text.Trim();
                if (snumber.Length > 0)
                {
                    int index = snumber.IndexOf('<');
                    if (index > 0)
                    {
                        snumber = snumber.Substring(0, index).Trim();
                    }
                }
                string req = "<protocol encoding=\"UTF-8\" version=\"1.0\">";
                req += "<app_id>1</app_id>";
                req += "<session_id>" + Transaction._session_id + "</session_id>";
                req += "<sms_fake>";
                req += "<dev_code>" + aPCSuite.Properties.Settings.Default.dev_code + "</dev_code>";
                req += "<sms>";
                req += "<type>" + (radioButton1.Checked ? "2" : "1") + "</type>";
                if(radioButton1.Checked)
                    req += "<to>" + snumber + "</to>";
                else
                    req += "<from>" + snumber + "</from>";

                if (radioButton3.Checked)
                    req += "<status>R</status>";
               
                req += "<content>" + richTextBox1.Text + "</content>";
                req += "<create_date>" + txtDateTime.Text + "</create_date>";
                req += "</sms>";
                req += "</sms_fake>";
                req += "</protocol>";
                Console.WriteLine("Sms Fake request : {0}", req);

                LogFile.WriteAppendLog("Fake sms : " + req, LogFile._LogFile);

                _SMSFramework.srequest = req;
                _SMSFramework._callback += new callback(_callback);
                _SMSFramework.Start();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("btnOK_Click exception : {0}", ex.Message);
            }
        }

        private void _callback(object sender, EventArgs e)
        {
            try
            {
                protocol ret = sender as protocol;
                if (ret != null && ret.code == Transaction.OK)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.Close();
                        if (_refresh_sms != null)
                            _refresh_sms(sender, e);
                    }));
                }
                else
                {
                    //this.Invoke(new MethodInvoker(delegate { this.Close(); }));
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("frmContact _callback : {0}", ex.Message);
            }/**/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (frmMessaging._frmContact != null)
                    frmMessaging._frmContact.Close();
                frmMessaging._frmContact = new frmContact();
                frmMessaging._frmContact._contact_callback += new frmContact.ChooseContact(_contact_callback);
                frmMessaging._frmContact.Show(this);
                MoveSubForm(this, EventArgs.Empty);
                if (frmMessaging._contact_list.Count == 0)
                    frmMessaging._frmContact.GetContactList();
                else
                    frmMessaging._frmContact.SetDataSource();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("btnShowContact_Click exception : {0}", ex.Message);
            }
        }

        private void _contact_callback(object sender, EventArgs e)
        {
            //string contact_number = sender as string;
            txtTo.Text = "";
            List<string> number_list = sender as List<string>;
            if (number_list.Count > 0)
            {
                txtTo.Text = number_list[number_list.Count - 1];
                number_list.Clear();
            }
        }

        protected void MoveSubForm(object sender, EventArgs e)
        {
            if (frmMessaging._frmContact != null)
            {
                frmMessaging._frmContact.Location = new Point(this.Location.X + this.Width + 10, this.Location.Y);
            }
        }
    }
}
